use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::models::entity::user;


/// 创建用户响应
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserCreated {
    /// 用户ID
    pub user_id: i32,
    /// 用户名
    pub username: String,
    /// 昵称
    pub nickname: String,
    /// 密码
    pub password: String,
}

/// 用户信息
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserInfo {
    /// 用户ID
    pub user_id: i32,
    /// 用户名
    pub username: String,
    /// 昵称
    pub nickname: String,
}

impl From<user::Model> for UserInfo {
    fn from(value: user::Model) -> Self {
        Self {
            user_id: value.id,
            username: value.username,
            nickname: value.nickname,
        }
    }
}

/// 用户详情
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserDetail {
    /// 用户ID
    pub user_id: i32,
    /// 用户名
    pub username: String,
    /// 昵称
    pub nickname: String,
    /// 状态
    pub status: i32,
    /// 创建时间
    pub create_time: String,
    /// 更新时间
    pub update_time: String,
}

impl From<user::Model> for UserDetail {
    fn from(value: user::Model) -> Self {
        let fmt = "%Y-%m-%d %H:%M:%S";
        Self {
            user_id: value.id,
            username: value.username,
            nickname: value.nickname,
            status: value.status,
            create_time: value.create_at.format(fmt).to_string(),
            update_time: value.update_at.format(fmt).to_string(),
        }
    }
}

/// 用户分页
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserPage {
    /// 用户ID
    pub user_id: i32,
    /// 用户名
    pub username: String,
    /// 昵称
    pub nickname: String,
    /// 状态
    pub status: i32,
    /// 创建时间
    pub create_time: String,
    /// 更新时间
    pub update_time: String,
}

impl From<user::Model> for UserPage {
    fn from(value: user::Model) -> Self {
        let fmt = "%Y-%m-%d %H-%M-%S";
        Self {
            user_id: value.id,
            username: value.username,
            nickname: value.nickname,
            status: value.status,
            create_time: value.create_at.format(fmt).to_string(),
            update_time: value.update_at.format(fmt).to_string(),
        }
    }
}
